﻿<template>
  <a-modal
    title="编辑物料信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入物料名称" v-decorator="['materialname', {rules: [{required: true, message: '请输入物料名称！'}]}]" />
        </a-form-item>
        <a-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入物料编号" v-decorator="['materialno', {rules: [{required: true, message: '请输入物料编号！'}]}]" />
        </a-form-item>
        <a-form-item label="物料批次" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入物料批次" v-decorator="['materialbatch', {rules: [{required: true, message: '请输入物料批次！'}]}]" />
        </a-form-item>
        <a-form-item label="检验方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择检验方式" v-decorator="['inspectionmethod', {rules: [{ required: true, message: '请选择检验方式！' }]}]">
            <a-select-option v-for="(item,index) in inspectionmethodData" :key="index" :value="parseInt(item.code)">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="物料类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择物料类别" v-decorator="['materialtype', {rules: [{ required: true, message: '请选择物料类别！' }]}]">
            <a-select-option v-for="(item,index) in materialtypeData" :key="index" :value="parseInt(item.code)">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入物料规格" v-decorator="['materialspec',{rules: [{required:true, message: '请输入物料规格'}]}]" />
        </a-form-item>
        <a-form-item label="单位类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group placeholder="请选择单位类别" v-decorator="['unittype']" >
            <a-radio v-for="(item,index) in unittypeData" :key="index" :value="parseInt(item.code)">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="单位编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group placeholder="请选择单位编号" v-decorator="['unitno']" >
            <a-radio v-for="(item,index) in unitnoData" :key="index" :value="parseInt(item.code)">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
		<a-form-item label="安全存量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
		  <a-input-number placeholder="请输入安全存量" style="width: 100%" v-decorator="['safeqty']" :min="0" />
		</a-form-item>
        <a-form-item label="是否关键物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择是否关键物料" v-decorator="['keymaterials', {rules: [{ required: true, message: '请选择是否关键物料！' }]}]">
            <a-select-option v-for="(item,index) in keymaterialsData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="保质期" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入保质期" style="width: 100%" v-decorator="['warranty']" :min="0" />
        </a-form-item>
        <a-form-item label="最大存量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入最大存量" style="width: 100%" v-decorator="['maximumqty']" :min="0" />
        </a-form-item>
        <a-form-item label="最小库龄" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入最小库龄" style="width: 100%" v-decorator="['minstorageage']" :min="0" />
        </a-form-item>
        <a-form-item label="最大库龄" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入最大库龄" style="width: 100%" v-decorator="['maxstorageage']" :min="0" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    WmsMaterialEdit
  } from '@/api/modular/main/WmsMaterialManage'
  export default {
    data () {
      return {
	    Id: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        inspectionmethodData: [],
        materialtypeData: [],
        unittypeData: [],
        unitnoData: [],
        keymaterialsData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true;
		this.Id = record.id;
        this.$nextTick(() => {
        });
        //深度拷贝 移除VUE的监听，防止INDEX页面值变动
        this.record = JSON.parse(JSON.stringify(record))
        const inspectionmethodOption = this.$options
        this.inspectionmethodData = inspectionmethodOption.filters['dictData']('material_inspection')
        const materialtypeOption = this.$options
        this.materialtypeData = materialtypeOption.filters['dictData']('material_type')
        const unittypeOption = this.$options
        this.unittypeData = unittypeOption.filters['dictData']('unit_type')
        const unitnoOption = this.$options
        this.unitnoData = unitnoOption.filters['dictData']('unitno_type')
        const keymaterialsOption = this.$options
        this.keymaterialsData = keymaterialsOption.filters['dictData']('yes_or_no')
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              materialname: record.materialname,
              materialno: record.materialno,
              materialbatch: record.materialbatch,
              inspectionmethod: record.inspectionmethod,
              materialtype: record.materialtype,
              materialspec: record.materialspec,
              unittype: record.unittype,
              unitno: record.unitno,
              safeqty: record.safeqty,
              keymaterials: record.keymaterials,
              warranty: record.warranty,
              maximumqty: record.maximumqty,
              minstorageage: record.minstorageage,
              maxstorageage: record.maxstorageage
            }
          )
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
		 //  if(values['warranty']<0){
			// this.$message.error('保质期不能未负数')
			// this.confirmLoading = false
			// return
		 //  }
          if (!errors) {
            for (const key in values) {
              if (values[key] == null) continue
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            WmsMaterialEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
